<template>
  <div class="admin">
    <!-- 位置显示 -->
    <div class='position'>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>系统管理</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/system/admin' }">超管管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 1. 搜索区域 -->
    <div class="header">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="超管账户名">
          <el-input v-model="formInline.account" placeholder="请输入超管账户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="findAccount()">查询</el-button>
          <el-button type="success" icon="el-icon-circle-plus-outline" @click="openAddDialog()">添加</el-button>
          <template>
            <download-excel class="export-excel-wrapper" :data="tableData" name="admin.xls">
              <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
              <el-button type="primary" icon="el-icon-download">导出数据</el-button>
            </download-excel>
          </template>
        </el-form-item>
      </el-form>
    </div>
    <!-- 2. 表格区域展示视图数据 -->
    <div class="wrapper">
      <el-table :data="tableData" border>
        <!-- <el-table-column type="selection" width="55"></el-table-column> -->
        <el-table-column prop="id" label="超管id" sortable width="auto"></el-table-column>
        <el-table-column prop="account" label="超管账户名" width="auto">
        </el-table-column>
        <el-table-column prop="password" label="超管密码" width="auto">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="auto">
        </el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="auto">
        </el-table-column>
        <el-table-column prop="version" label="修改次数" width="auto">
        </el-table-column>
        <el-table-column prop="status" label="使用状态" width="auto">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.status" effect="dark">正常</el-tag>
            <el-tag type="info" effect="dark" v-else>停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="findDate(scope.row)">查看</el-button>
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)" icon="el-icon-edit">
              编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 3. 分页 -->
    <MyPagination :total="total" :pageSize="pageSize" @SizeChange="SizeChange" @Pagechange="Pagechange" :currentPage="currentPage" />
    <!-- 查看数据 -->
    <el-dialog title="查看管理员信息" :visible.sync="MessageDalog" width="50%" :before-close="handleClose">
      <el-table :data="rowData">
        <el-table-column prop="id" label="超管id" width="auto"></el-table-column>
        <el-table-column prop="account" label="超管账户名" width="auto"></el-table-column>
        <el-table-column prop="password" label="超管密码" width="auto"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="auto"></el-table-column>
        <el-table-column prop="updateTime" label="修改时间" width="auto"></el-table-column>
        <el-table-column prop="version" label="修改次数" width="auto"></el-table-column>
        <el-table-column prop="status" label="使用状态" width="100">
          <template slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.status" effect="dark">正常</el-tag>
            <el-tag type="info" effect="dark" v-else>停用</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 添加 -->
    <el-dialog title="添加管理员" :visible.sync="addDialog">
      <el-form :model="addfrom">
        <el-form-item label="账户名" label-width="120px">
          <el-input v-model.trim="addfrom.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input type="password" v-model="addfrom.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAddAdmin()">取 消</el-button>
        <el-button type="primary" @click="submitAddFrom()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改dialog -->
    <el-dialog title="修改管理员" :visible.sync="updateDialog">
      <el-form :model="updatefrom">
        <el-form-item label="账户名" label-width="120px">
          <el-input v-model.trim="updatefrom.account" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="updatefrom.password" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="updateDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitUpdateFrom()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import { getPageAll, addAdmin, updateAdmin, deleteAdmin } from '@/api/api'
import MyPagination from "../components/MyPagination.vue";
const AccountRule = /^[a-zA-Z][a-zA-Z0-9]{4,10}$/   //账户名正则 首位必须是字母，含有字母和数字，5~11位字符
const PasswordRule = /^[a-zA-Z0-9]{4,16}$/    //密码正则 4到16位（字母，数字）
export default {
  components: {
    MyPagination,
  },
  data() {
    return {
      formInline: {
        //账户名
        account: "",
      },
      tableData: [],
      total: 5,
      pageSize: 5,//一页几条数据
      currentPage: 1, //选中的高亮页码
      //从这开始是查看数据的暂用变量
      MessageDalog: false,
      rowData: [],//行数据
      //从这开始是添加数据的暂用变量
      addDialog: false,
      addfrom: {
        account: '',
        password: ''
      },
      //从这开始是修改
      updateDialog: false,
      oldData: {},//保存待修改的对象的值
      updatefrom: {
        account: '',
        password: ''
      }
    }
  },
  mounted() {
    this.loadData()
  },
  methods: {
    loadData() {
      getPageAll({ pageSize: this.pageSize, pageNum: this.currentPage, account: this.formInline.account }).then((result) => {
        console.log(result);
        this.tableData = result.data.list
        this.total = result.data.total
        this.pageSize = result.data.pageSize
        this.currentPage = result.data.pageNum
      })
    },
    findAccount() {
      if (!this.formInline.account) return this.$message.error('输入数据为空无法查询')
      this.loadData()
      this.$message.success('查询成功!共' + this.total + '条数据!')
      this.formInline.account = ''
    },
    Pagechange(pageNum) {//改变页码
      console.log(pageNum);
      this.currentPage = pageNum
      this.loadData()
    },
    SizeChange(pageSize) {//改变一页有几条
      console.log(pageSize);
      this.pageSize = pageSize
      this.loadData()
    },
    findDate(row) {//打开每行数据dialog
      console.log(row);
      this.rowData.push(row)//添加一行数据到rowdata变量
      this.MessageDalog = true//显示对话框
    },
    handleClose() {//关闭对话框
      this.MessageDalog = false
      this.rowData = []//清空数据
    },
    openAddDialog() {//打开新增dialog
      this.addDialog = true
    },
    cancelAddAdmin() {//关闭dialog 并且清空输入框
      this.$message.info('取消新增')
      this.addDialog = false
      this.addfrom.account = ''
      this.addfrom.password = ''
    },
    submitAddFrom() {
      if (this.addfrom.account == '' || this.addfrom.password == '') return this.$message.error('请输入完整的信息')

      if (AccountRule.test(this.addfrom.account)) {//正则验证用户名
        this.handleAddAdmin()
      } else {
        return this.$message.error('用户名必须4到16位中英文数字')
      }
    },
    handleAddAdmin() {//新增请求接口
      addAdmin({ account: this.addfrom.account, password: this.addfrom.password }).then(res => {
        console.log(res);
        if (res.status == 200) {
          this.$message.success('添加成功')
          this.addfrom.account = ''
          this.addfrom.password = ''
          this.addDialog = false
          this.loadData()
        } else {
          return this.$message.error(res.msg)
        }
      })
    },
    handleEdit(data) {
      this.updateDialog = true
      this.updatefrom.account = data.account
      this.updatefrom.password = data.password
      this.oldData = data//保存就该瘴之前的数据
    },
    submitUpdateFrom() {//提交修改
      if (this.updatefrom.account === this.oldData.account && this.updatefrom.password === this.oldData.password) {
        return this.$message.error('与原来相同你修改个锤子')
      }
      if (AccountRule.test(this.updatefrom.account) && PasswordRule.test(this.updatefrom.password)) {
        updateAdmin({ id: this.oldData.id, account: this.updatefrom.account, password: this.updatefrom.password }).then(res => {
          console.log(res);
          if (res.status == 200) {
            this.$message.success(res.msg)
            this.loadData()
          } else {
            this.$message.error(res.msg)
          }
        })
      } else {
        return this.$message.error('您要修改信息的不符合规范')
      }
    },
    handleDelete(data) {//删除
      this.$confirm('确认删除此用户吗?')
        .then(_ => {
          deleteAdmin({ id: data.id }).then(res => {
            console.log(res);
            if (res.status == 200) {
              this.$message.success(res.msg)
              this.loadData()
            } else {
              this.$message.error(res.msg)
            }
          })
        })
        .catch(_ => {
          this.$message.info('取消删除')
        });
    }
  }
}
</script>

<style lang='less' scoped>
.export-excel-wrapper {
  padding: 0;
  margin: 0;
  float: right;
}
.position {
  // background: #fff;
  padding: 10px;
  // border: 1px solid #eee;
  margin-bottom: 10px;
}
.header {
  display: flex;
  background: #fff;
  padding: 10px;
  border: 1px solid #eee;
  button {
    margin-left: 20px;
  }
  .el-form-item {
    margin-bottom: 0;
  }
}
.wrapper {
  margin: 10px 0;
}
</style>